﻿using System;

namespace Besture.UI.Admin.Control
{
    public partial class UploadSingleFile : System.Web.UI.UserControl
    {
        /// <summary>
        /// DIV ID
        /// </summary>
        public string FileDivID = Guid.NewGuid().ToString();
        /// <summary>
        /// 上传文件路径
        /// </summary>
        public string FolderPath { get; set; }
        /// <summary>
        /// 是否必须上传
        /// </summary>
        public bool Required { get; set; }
        /// <summary>
        /// 必须上传文件
        /// </summary>
        public string RequiredFileUrl = "/admin/Images/dispensable.png";
        /// <summary>
        /// 文件上传限制
        /// </summary>
        public string FileTypeExts { get; set; }
        /// <summary>
        /// 文件上传限制说明
        /// </summary>
        public string FileTypeDesc { get; set; }

        /// <summary>
        /// 文件地址
        /// </summary>
        public string FileUrl
        {
            get { return txtFileUrl.Text; }
            set { txtFileUrl.Text = value; }
        }

        /// <summary>
        /// 临时文件文件夹
        /// </summary>
        public string folderPathTemp;

        public string InitText = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtFileUrl.Text = FileUrl;

                //必须上传
                if (Required)
                {
                    txtFileUrl.CssClass = "FileRequired";
                    RequiredFileUrl = "/admin/Images/indispensable.gif";
                }

                InitText = InitPhotoDiv();
            }
        }


        /// <summary>
        /// 初始化文件HTML
        /// </summary>
        /// <returns></returns>
        protected string InitPhotoDiv()
        {
            string text = string.Empty;
            if (!string.IsNullOrEmpty(FileUrl))
            {
                string fileName = FileUrl;
                if (FileUrl.IndexOf('/')>-1)
                {
                    fileName = FileUrl.Split('/')[1];
                }
                text = text + ("<div class='element' id='" + FileDivID + "_div'>");
                text = text + ("    <div>");
                text = text + ("         已存在文件：【<a href='" + FolderPath + FileUrl + "'>" + fileName + "</a>】,右键另存可下载！");
                text = text + ("    </div>");
                text = text + ("</div>");
            }
            return text;
        }
    }
}